#include "tcpclientui.h"
#include <QMessageBox>
#include <iostream>
using namespace std;

TcpClientUI::TcpClientUI(QWidget *parent)
    : QDialog(parent)
{
    status = false;
    port = 8010;

    setWindowTitle(tr("TCP Client"));
    contentListWidget = new QListWidget;
    sendMsg = new QLineEdit;
    sendBtn = new QPushButton(tr("发送"));
    nameLabel = new QLabel(tr("昵称："));
    nameLineEdit = new QLineEdit;
    serverAddrLabel = new QLabel(tr("服务器地址："));
    serverAddrLineEdit = new QLineEdit;
    serverPortLabel = new QLabel(tr("端口："));
    serverPortLineEdit = new QLineEdit(QString::number(port));
    connectBtn = new QPushButton(tr("进入聊天室"));
    mainLayout = new QGridLayout(this);

    clientSocket = new Client(this);

    //绑定UI上的信号和client的槽
    //connectedServer信号将三个QString传入clientSocket的slotConnectServer槽
    connect(this, SIGNAL(connectServer(QString, QString, QString)),
            clientSocket, SLOT(slotConnectServer(QString, QString, QString)));
    connect(this, SIGNAL(sendServer(QString)),
            clientSocket, SLOT(slotSendServer(QString)));
    connect(this, SIGNAL(disconnectServer()),
            clientSocket, SLOT(slotDisconnectServer()));

    //将clientdSocket的disconnected信号与UI的slotDisconnected槽绑定
    connect(clientSocket, SIGNAL(disconnected()),
            this, SLOT(slotDisconnected()));
    //如果client接受到数据，更新UI
    //绑定clientSocket的updateClientsUI信号和UI的updateClientUI槽
    connect(clientSocket, SIGNAL(updateClientUI(QString, int)),
            this, SLOT(updateClientUI(QString, int)));

    mainLayout->addWidget(contentListWidget, 0, 0, 1, 2);
    mainLayout->addWidget(sendMsg, 1, 0);
    mainLayout->addWidget(sendBtn, 1, 1);
    mainLayout->addWidget(nameLabel, 2, 0);
    mainLayout->addWidget(nameLineEdit, 2, 1);
    mainLayout->addWidget(serverAddrLabel, 3, 0);
    mainLayout->addWidget(serverAddrLineEdit, 3, 1);
    mainLayout->addWidget(serverPortLabel, 4, 0);
    mainLayout->addWidget(serverPortLineEdit, 4, 1);
    mainLayout->addWidget(connectBtn, 5, 0, 1, 2);

    //将UI的button与UI的一些信号绑定
    connect(connectBtn, SIGNAL(clicked()), this, SLOT(slotCreateServer()));
    connect(sendBtn, SIGNAL(clicked()), this, SLOT(slotSendServer()));
}

void TcpClientUI::slotCreateServer()
{
    //status判断是否已经与server连接，如果已连接，则断连；如果未连接，建立连接
    if(!status)
    {
        QString name = nameLineEdit->text();
        QString IP = serverAddrLineEdit->text();
        QString port = serverPortLineEdit->text();
        if(name == "" || IP == "" || port == "")
        {
            QMessageBox::information(this, tr("error"), tr("error!"));
            return;
        }
        status = true;
        connectBtn->setText(tr("离开聊天室"));
        emit connectServer(name, IP, port);
    }
    else
    {
        emit disconnectServer();
    }
}

void TcpClientUI::slotSendServer()
{
    cout<<"send heihei"<<endl;
    QString msg = sendMsg->text();
    if(msg == "")
        return;
    sendMsg->clear();
    //触发sendServer信号，client的一个槽会获取msg数据
    emit sendServer(msg);
}

void TcpClientUI::slotDisconnected()
{
    status = false;
    connectBtn->setText(tr("进入聊天室"));
}

void TcpClientUI::updateClientUI(QString msg, int length)
{
    contentListWidget->addItem(msg.left(length));
}

TcpClientUI::~TcpClientUI()
{

}
